package com.hnucm.c201901020121.Fragment.My;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.hnucm.c201901020121.Activity.TaskDetailAct;
import com.hnucm.c201901020121.R;
import com.hnucm.c201901020121.Utils.Bmob.BMyTask;
import com.hnucm.c201901020121.Utils.Interfaces.OnMyTaskRecyclerItemClickListener;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.datatype.BmobQueryResult;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SQLQueryListener;

import static com.hnucm.c201901020121.Activity.LoginAct.currentUser;

public class AuditFinishedFragment extends Fragment {
    RecyclerView recyclerView;
    List<BMyTask> myTaskList = new ArrayList<>();
    MyAdapter myAdapter;
    private OnMyTaskRecyclerItemClickListener onMyTaskRecyclerItemClickListener;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_audio_unqualified, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        recyclerView = getActivity().findViewById(R.id.unqualirecycleview_mystask2);
        myAdapter = new MyAdapter();
        myAdapter.setRecyclerItemClickListener(new OnMyTaskRecyclerItemClickListener() {
            @Override
            public void onItemClick(int Position, List<BMyTask> taskList) {
                Intent intent = new Intent(getContext(), TaskDetailAct.class);
                intent.putExtra("taskId", taskList.get(Position).getTaskId());
                startActivity(intent);
            }
        });
        recyclerView.setAdapter(myAdapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        getDate();
    }

    private void getDate() {
        String sql =  "select * from BMyTask where publisherId="+"'"+currentUser.getObjectId()+"'"+" and state='已完成'";
        BmobQuery<BMyTask> query = new BmobQuery<BMyTask>();
        query.setSQL(sql);
        query.doSQLQuery(new SQLQueryListener<BMyTask>() {
            @RequiresApi(api = Build.VERSION_CODES.N)
            @Override
            public void done(BmobQueryResult<BMyTask> bmobQueryResult, BmobException e) {
                if (e == null) {
                    List<BMyTask> list = (List<BMyTask>) bmobQueryResult.getResults();
                    //Log.i("Bmob", list.get(0).getObjectId());
                    myTaskList.clear();
                    myTaskList.addAll(list);
                    myAdapter.notifyDataSetChanged();//刷新列表数据
                } else {
                    Log.i("Bmob", "查找失败");
                }
            }
        });
    }

    public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
        public void setRecyclerItemClickListener(OnMyTaskRecyclerItemClickListener listener) {
            onMyTaskRecyclerItemClickListener = listener;
        }

        @NonNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(getActivity()).inflate(R.layout.item_my_task, parent, false);
            return new  MyViewHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
            BMyTask chat = myTaskList.get(position);
            MyViewHolder holder1 = ( MyViewHolder) holder;
            holder1.title.setText(myTaskList.get(position).getTitle());
            holder1.time.setText(myTaskList.get(position).getCreatedAt());
            holder1.label.setText(myTaskList.get(position).getLabel());
            holder1.state.setText(myTaskList.get(position).getState());
            holder1.amount.setText(myTaskList.get(position).getAmount());
            Glide.with(getActivity()).
                    load(myTaskList.get(position).getHeadimg())
                    .into(holder1.headimg);
        }

        @Override
        public int getItemCount() {
            return myTaskList.size();
        }
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        TextView title, time, label, state, amount;
        ImageView headimg;
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            title = itemView.findViewById(R.id.textView101);
            time = itemView.findViewById(R.id.textView104);
            label = itemView.findViewById(R.id.textView102);
            state = itemView.findViewById(R.id.textView112);
            amount = itemView.findViewById(R.id.textView113);
            headimg = itemView.findViewById(R.id.circleImageView5);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onMyTaskRecyclerItemClickListener != null) {
                        onMyTaskRecyclerItemClickListener.onItemClick(getAdapterPosition(), myTaskList);
                    }
                }
            });
        }
    }

}